/** 
Copyright (c) 2010, Sheikh Abdul Wahid Ahmed
Details @ http://extremecodeworld.codeplex.com/license
**/

using System;
using EXtremecode.Utils;
using System.Data;

namespace EXtremecode.DataAccessLayer
{
	/// <summary>
	/// Summary description for DataRowReader.
	/// </summary>
	public class DataRowReader:IDataReader
	{
		private DataRow[] drs;
		private int currentIndex=-1;
		private int startIndex;
		private int endIndex;
		public DataRowReader(DataRow[] drs,int pageNumber,int pageSize)
		{
			this.drs=drs;

            if (drs != null)
            {
                if (pageNumber <= 0)
                {
                    //no paging return all rows.
                    startIndex = 0;
                    endIndex = drs.Length - 1;
                }
                else
                {
                    startIndex = (pageNumber - 1) * pageSize;// ((2-1) * 10)= 10 --> 19

                    int temp = startIndex + pageSize;
                    endIndex = (temp > drs.Length) ? drs.Length - 1 : temp - 1;
                }
            }
            else
            {
                startIndex = endIndex = -1;
            }

			currentIndex= startIndex-1; // before the first index.  

		}

		public  DataRow CurrentDataRow
		{
			get
			{
				return drs[currentIndex];
			}
		}



		#region IDataReader Members

		public int RecordsAffected
		{
			get
			{
				return (drs == null)? 0: drs.Length;
				//used for getting row count with ignorance of paging.
			}
		}

		public bool IsClosed
		{
			get
			{
				// TODO:  Add DataRowReader.IsClosed getter implementation
				return false;
			}
		}

		public bool NextResult()
		{
			// TODO:  Add DataRowReader.NextResult implementation
			return false;
		}

		public void Close()
		{
			// TODO:  Add DataRowReader.Close implementation
		}

		public bool Read()
		{
			currentIndex++;
			return currentIndex<=endIndex; 
		}

		public int Depth
		{
			get
			{
				// TODO:  Add DataRowReader.Depth getter implementation
				return 0;
			}
		}

		public DataTable GetSchemaTable()
		{
			// TODO:  Add DataRowReader.GetSchemaTable implementation
			return null;
		}

		#endregion

		#region IDisposable Members

		public void Dispose()
		{
			// TODO:  Add DataRowReader.Dispose implementation
		}

		#endregion

		#region IDataRecord Members

		public int GetInt32(int i)
		{
			// TODO:  Add DataRowReader.GetInt32 implementation
			return 0;
		}

		public object this[string name]
		{
			get
			{
				return drs[currentIndex][name];
			}
		}

		object System.Data.IDataRecord.this[int i]
		{
			get
			{
				
				return drs[currentIndex][i];
			}
		}

		public object GetValue(int i)
		{
			// TODO:  Add DataRowReader.GetValue implementation
			return null;
		}

		public bool IsDBNull(int i)
		{
			// TODO:  Add DataRowReader.IsDBNull implementation
			return false;
		}

		public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)
		{
			// TODO:  Add DataRowReader.GetBytes implementation
			return 0;
		}

		public byte GetByte(int i)
		{
			// TODO:  Add DataRowReader.GetByte implementation
			return 0;
		}

		public Type GetFieldType(int i)
		{
			// TODO:  Add DataRowReader.GetFieldType implementation
			return null;
		}

		public decimal GetDecimal(int i)
		{
			// TODO:  Add DataRowReader.GetDecimal implementation
			return 0;
		}

		public int GetValues(object[] values)
		{
			// TODO:  Add DataRowReader.GetValues implementation
			return 0;
		}

		public string GetName(int i)
		{
			// TODO:  Add DataRowReader.GetName implementation
			return null;
		}

		public int FieldCount
		{
			get
			{
				// TODO:  Add DataRowReader.FieldCount getter implementation
				return 0;
			}
		}

		public long GetInt64(int i)
		{
			// TODO:  Add DataRowReader.GetInt64 implementation
			return 0;
		}

		public double GetDouble(int i)
		{
			// TODO:  Add DataRowReader.GetDouble implementation
			return 0;
		}

		public bool GetBoolean(int i)
		{
			// TODO:  Add DataRowReader.GetBoolean implementation
			return false;
		}

		public Guid GetGuid(int i)
		{
			// TODO:  Add DataRowReader.GetGuid implementation
			return new Guid ();
		}

		public DateTime GetDateTime(int i)
		{
			// TODO:  Add DataRowReader.GetDateTime implementation
			return new DateTime ();
		}

		public int GetOrdinal(string name)
		{
			// TODO:  Add DataRowReader.GetOrdinal implementation
			return 0;
		}

		public string GetDataTypeName(int i)
		{
			// TODO:  Add DataRowReader.GetDataTypeName implementation
			return null;
		}

		public float GetFloat(int i)
		{
			// TODO:  Add DataRowReader.GetFloat implementation
			return 0;
		}

		public IDataReader GetData(int i)
		{
			// TODO:  Add DataRowReader.GetData implementation
			return null;
		}

		public long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length)
		{
			// TODO:  Add DataRowReader.GetChars implementation
			return 0;
		}

		public string GetString(int i)
		{
			// TODO:  Add DataRowReader.GetString implementation
			return null;
		}

		public char GetChar(int i)
		{
			// TODO:  Add DataRowReader.GetChar implementation
			return '\0';
		}

		public short GetInt16(int i)
		{
			// TODO:  Add DataRowReader.GetInt16 implementation
			return 0;
		}

		#endregion
	}
}
